package cn.szzsi.service;

import cn.szzsi.common.model.Member;
import cn.szzsi.common.model.MemberRank;
import cn.szzsi.common.model.UserBalanceDetail;

import java.math.BigDecimal;

/**
 * zhengy
 */
public class MemberRankService {
    public static final MemberRankService service = new MemberRankService();


    /**
     * 更新会员等级
     * @param member 会员
     */
    public void updateMemberRank(Member member){
        if(member!=null){
            String shopId=member.getShopId();//商户id
            //1.查询该会员总共消费了多少金额
            BigDecimal amountMoney= UserBalanceDetailService.service.getAmountBalanceByMidAndType(member.getId(), UserBalanceDetail.BalanceType.memberPayment.ordinal());

            //2.根据会员消费金额获取该会员所在等级
            MemberRank memberRank= MemberRank.dao.findFirst("select id from mall_member_rank where shopId=? and moneyNum <=? order by moneyNum desc",shopId,amountMoney);
            if(memberRank!=null){//更改会员等级
                String memberRankId=memberRank.getId();
                member.setMemberRankId(memberRankId);
                member.update();
            }


        }

    }
}
